/*
 * (C) 2012 Sheng Yi
 * Distinguish the system task and user task
 */
#include <stdio.h>

void scheduler(int task[], int n, int system_task[], int user_task[]);
void insert(int task[], int task_index[], int pos, int val);

int main()
{
	int task[9] = {0, 30, 155, 1, 80, 300, 170, 40, 99};
	int sys[9] = {0};
	int user[9] = {0};
	int i;
	for (i = 0; i < 9; i++)
		printf("%d ", task[i]);
	printf("\n");

	scheduler(task, 9, sys, user);
	for (i = 0; i < 9; i++)
		printf("%d ", sys[i]);
	printf("\n");


	for (i = 0; i < 9; i++)
		printf("%d ", user[i]);
	printf("\n");

	return 0;
}

void scheduler(int task[], int n, int system_task[], int user_task[])
{
	int sys_pos = -1, user_pos = -1;

	int i;
	for (i = 0; i < n; i++)
	{
		if (task[i] < 50) // system task
		{
			// insert to system task
			insert(task, system_task, sys_pos, i);
			sys_pos++;
		}
		else if (task[i] >= 50 && task[i] <= 255) // user task
		{
			// insert to user task
			insert(task, user_task, user_pos, i);
			user_pos++;
		}
		else
			; // illegal task, ignore
	}

	system_task[++sys_pos] = -1;
	user_task[++user_pos] = -1;
}

void insert(int task[], int task_index[], int pos, int val)
{
	while (pos >= 0)
	{
		if (task[task_index[pos]] > task[val])
		{
			task_index[pos + 1] = task_index[pos];
			pos--;
		}
		else
			break;
	}

	task_index[pos + 1] = val;
}
